package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Service.EmployeeService;
import com.sg.hms.ServiceImpl.EmployeeServiceImpl;

import net.sf.json.JSONObject;

/**
 * 
 * @author 陈良机
 *
 */
public class SelectEmplController extends HttpServlet{
	EmployeeService em=new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	req.setCharacterEncoding("utf8");
    	resp.setCharacterEncoding("utf8");
    	int page=Integer.parseInt(req.getParameter("page"));//展示第几页
    	int rows=Integer.parseInt(req.getParameter("rows"));//每页几条数据
    	int start=(page-1)*rows;//每页开始的位置
    	String searchName=req.getParameter("searchelName");//获取搜索关键字
    	String order=req.getParameter("order");//升序还是降序（asc、desc）
    	String sort=req.getParameter("sort");//获取要排序的列
    	JSONObject emjson=new JSONObject();
    	if(searchName!=null){//搜索关键字不为空
    		emjson=em.selectEmloyeeByGroup(start, rows, searchName);
    		if(sort!=null){//搜索之后排序
    			emjson=em.SortEmpl(searchName, sort, order, start, rows);
    		}
    	}
    	//没有模糊查询的情况下排序
    	else if(order!=null){
    		emjson=em.sortEmpl(sort, order, start, rows);
    	}
    	//显示初始表格
    	else{
    		emjson= em.selectEmloyeeByGroup(start, rows);
    	}
    	PrintWriter pw=resp.getWriter();
    	pw.write(emjson.toString());
    }
}
